package service

import (
	"context"
	"errors"
	"user-srv/basic/global"
	__ "user-srv/basic/proto/good"
	"user-srv/handler/model"
)

// 商品添加
func (s *Server) GoodCreate(_ context.Context, in *__.GoodCreateReq) (*__.GoodCreateResp, error) {

	good := model.Good{
		GoodName:    in.GoodName,
		GoodClass:   int(in.GoodClass),
		Price:       float32(in.Price),
		Stock:       int(in.Stock),
		Description: in.Description,
		ImageURL:    in.ImageURL,
		Status:      int(in.Status),
	}
	if err := good.GoodCreate(global.DB); err != nil {
		return nil, errors.New("商品添加失败")
	}
	return &__.GoodCreateResp{
		Id: int64(good.ID),
	}, nil
}

// 商品修改
func (s *Server) GoodUpdate(_ context.Context, in *__.GoodUpdateReq) (*__.GoodUpdateResp, error) {

	good := model.Good{
		GoodName:    in.GoodName,
		GoodClass:   int(in.GoodClass),
		Price:       float32(in.Price),
		Stock:       int(in.Stock),
		Description: in.Description,
		ImageURL:    in.ImageURL,
		Status:      int(in.Status),
	}
	if err := good.GoodUpdate(global.DB, in.Id); err != nil {
		return nil, errors.New("商品修改失败")
	}
	return &__.GoodUpdateResp{
		Id: int64(good.ID),
	}, nil
}
